package com.dream.service.user.util;

import com.alibaba.dubbo.common.utils.StringUtils;


/**
 * Created by kangd on 2016/8/16.
 */
public class UserInviteCode {

    private static int LOWER_CASE_ASCII = 97 ;   // 小写 ASCII

    private static int BUFFER_BIT=10; // 数字转字母，buffer （buf[]）数组位数

    private static String INVITE_CODE_PREFIX = "m";  //邀请码前缀

    private static int INVITE_CODE_NUM_BASE = 100; //邀请码数字位基数

    /**
     * userId 真实差值
     */
    private static int OMIT_LEN = 1000000;

    public static String convertToInviteCode(Integer userId){
        if (userId == null) return null;
        userId = userId - OMIT_LEN;
        /**
         * user id 转为 invite code 逻辑：
         */
        String inviteCode = INVITE_CODE_PREFIX + (userId % INVITE_CODE_NUM_BASE < 10? ("0"+userId % INVITE_CODE_NUM_BASE):userId % INVITE_CODE_NUM_BASE) + toRadix(userId/100);
        return inviteCode;
    }

    public static Integer convertToUserId(String inviteCode){
        if(StringUtils.isBlank(inviteCode)) return null;
        if(inviteCode.indexOf(INVITE_CODE_PREFIX) != 0){
            return null;
        }
        inviteCode = inviteCode.substring(INVITE_CODE_PREFIX.length());
        Integer userId = Integer.valueOf(inviteCode.substring(0,(int)Math.log10(INVITE_CODE_NUM_BASE)));
        userId = userId + toNum(inviteCode.substring((int)Math.log10(INVITE_CODE_NUM_BASE))) * 100;
        return userId+OMIT_LEN;
    }

    public static void main(String[] args) {
        long s = System.currentTimeMillis();
        for(int i = 1000000 ; i<=1010010; i++){
            System.out.println("i:"+i+","+convertToInviteCode(i));
            if(convertToUserId(convertToInviteCode(i))!=i){
                System.out.println(i);
            }
        }
        System.out.println(System.currentTimeMillis() -s);
        System.out.println(convertToInviteCode(1000069));
    }


    public static String toRadix(int data,int radix){
        char buf[] = new char[BUFFER_BIT];
        int charPos = BUFFER_BIT-1;
        while(data>=radix){
            buf[charPos--]=(char) (data % radix+ LOWER_CASE_ASCII);
            data=data/radix;
        }
        buf[charPos] = (char) (data +LOWER_CASE_ASCII);
        return new String(buf, charPos, (BUFFER_BIT - charPos));
    }

    public static String toRadix(int data){
        return toRadix(data,26);
    }


    public static Integer toNum(String data,int radix){
        if(data == null) return null;
        Integer num = 0;
        char buf[] = data.toCharArray();
        for(int i=0;i<buf.length;i++){
            num = num + (buf[i]-'a') * (int)Math.pow(radix,buf.length - 1 - i);
        }
        return num;
    }

    public static Integer toNum(String data){
        return toNum(data,26);
    }


    public static String toRadixByArray(int data,int radix){
        char[] digits={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        char buf[] = new char[BUFFER_BIT];
        int charPos = BUFFER_BIT-1;
        while(data>=radix){
            buf[charPos--]=digits[data % radix];
            data=data/radix;
        }
        buf[charPos] = digits[data];
        return new String(buf, charPos, (BUFFER_BIT - charPos));
    }

    public static String toRadixByArray(int data){
        return toRadixByArray(data, 26);
    }

}
